//
//  MSWavaViewController.m
//  Wave
//
//  Created by admin on 15/7/4.
//  Copyright (c) 2015年 MLS. All rights reserved.
//

#import "MSWavaViewController.h"

@interface MSWavaViewController ()

@end

@implementation MSWavaViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"shake" ofType:@"wav"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);
}
- (IBAction)motionBegin:(id)sender
{
    [self addAnimations];
    AudioServicesPlaySystemSound (soundID);
//    AudioServicesPlaySystemSound(soundID);
}

// 开始摇晃时调用
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake)
    {
        [self addAnimations];
        AudioServicesPlaySystemSound (soundID);
    }
}
// 摇晃结束时调用
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 此处进行想要的操作
    NSLog(@"数据获取");
}
// 取消时调用
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    
}
-(BOOL)canBecomeFirstResponder
{
    return YES;
}


#pragma mark - 动画
- (void)addAnimations
{
    CABasicAnimation *translation = [CABasicAnimation animationWithKeyPath:@"transform"];
    translation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    translation.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_1_PI, 0, 0, 100)];
    
    translation.duration = 0.2;
    translation.repeatCount = 2;
    translation.autoreverses = YES;
    
    [_wavaBtn.layer addAnimation:translation forKey:@"translation"];
    
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
    
    [self performSelector:@selector(rockRequest) withObject:self afterDelay:1.5];
    
}

#pragma mark - 数据获取
- (void)rockRequest
{
    // 此处进行想要的操作
    NSLog(@"数据获取");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
